package at.fhooe.engine.physics;

import java.util.ArrayList;
import java.util.List;

import at.fhooe.engine.event.events.CollisionEvent;
import at.fhooe.engine.pandora.Game;

public class PhysicsManager {

	private List<PhysicBody> m_bodies;
	
	public PhysicsManager() {
		m_bodies = new ArrayList<PhysicBody>();
	}
	
	public void addPhysicBody(PhysicBody physicBody) {
		m_bodies.add(physicBody);
	}
	
	public void removePhysicBody(PhysicBody physicBody) {
		m_bodies.remove(physicBody);
	}
	
	public void update() {
		for (PhysicBody physicBody : m_bodies) {
			physicBody.update();
		}
		collisionTest();
	}

	private void collisionTest() {
		for(int i = 0; i < m_bodies.size(); i++) {
			PhysicBody body1 = m_bodies.get(i);
			for(int j = i + 1; j < m_bodies.size(); j++) {
				PhysicBody body2 = m_bodies.get(j);
				
				if(body1.isColliding(body2)) {
					Game.getInstance().getEventManager().dispatchEvent(new CollisionEvent(body1.getName(), body2.getName()));
				}
			}
		}
		
	}
}
